package cn.kgc.controller;

import cn.kgc.pojo.*;
/*import cn.kgc.pojo.AppCategory;
import cn.kgc.pojo.AppInfo;
import cn.kgc.pojo.DataDictionary;
import cn.kgc.pojo.WXWAppInfo;*/
import cn.kgc.service.WXWdictionaryService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author: wang
 * @date: 2022/1/4
 * @description:
 */
@Controller
@RequestMapping("/wxw")
public class WXWcontroller {

    Logger logger = Logger.getLogger(WXWcontroller.class);

    @Resource
    private WXWdictionaryService wxWdictionaryService;

    @RequestMapping("/toapp")
    public String toApp(){
        return "developer/appinfolist";
    }

    @GetMapping("/selapp")
    @ResponseBody
    public String selApp(){
        List<DataDictionary> data = wxWdictionaryService.selApp();
        return JSONObject.toJSONString(data);
    }

    @GetMapping("/selping")
    @ResponseBody
    public String selPing(){
        List<DataDictionary> data = wxWdictionaryService.selPing();
        return JSONObject.toJSONString(data);
    }

    @GetMapping("/selYi")
    @ResponseBody
    public String selYi(){
        List<AppCategory> appCategories = wxWdictionaryService.selYi();
        return JSONObject.toJSONString(appCategories);
    }
    @GetMapping("/selEr")
    @ResponseBody
    public String selEr(Integer id){
        List<AppCategory> appCategories = wxWdictionaryService.selEr(id);
        return JSONObject.toJSONString(appCategories);
    }
    @GetMapping("/selSan")
    @ResponseBody
    public String selSan(Integer id){
        List<AppCategory> appCategories = wxWdictionaryService.selSan(id);
        return JSONObject.toJSONString(appCategories);
    }

    @GetMapping("/selList")
    @ResponseBody
    public String selList(String querySoftwareName,String queryStatus,String queryFlatformId,
                          String queryCategoryLevel1,String queryCategoryLevel2,
                          String queryCategoryLevel3,
                          Integer currentPage,Integer pageSize){
        HashMap<String ,Object> map = new HashMap<String,Object>();
        map.put("softwareName","%"+querySoftwareName+"%");
        map.put("status",queryStatus);
        map.put("flatformId",queryFlatformId);
        map.put("categoryLevel1",queryCategoryLevel1);
        map.put("categoryLevel2",queryCategoryLevel2);
        map.put("categoryLevel3",queryCategoryLevel3);
        PageHelper.startPage(currentPage,pageSize);
        Page<WXWAppInfo> wxwAppInfos = wxWdictionaryService.selFen(map);
        //System.out.println("总页数"+wxwAppInfos.getPages());
        //System.out.println("总条数"+wxwAppInfos.getTotal());
        PageInfo<WXWAppInfo> pageInfo = new PageInfo<WXWAppInfo>(wxwAppInfos);
        return JSONObject.toJSONString(pageInfo);
    }
    @RequestMapping("/updaInfo")
    public String toUpd(){
        return "developer/appinfomodify";
    }
    @RequestMapping("/toView")
    public String toView(){
        return "developer/appinfoview";
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "developer/appversionadd";
    }
    @RequestMapping("/toUpdBan")
    public String toUpdBan(){
        return "developer/appversionmodify";
    }

    @GetMapping("/updaInfoShang")
    @ResponseBody
    public String updaInfoShang(Integer id){
        boolean b = wxWdictionaryService.updInfoShang(id);
        return JSONObject.toJSONString(b);
    }
    @GetMapping("/updaInfoXia")
    @ResponseBody
    public String updaInfoXia(Integer id){
        boolean b = wxWdictionaryService.updInfoXia(id);
        return JSONObject.toJSONString(b);
    }

    @GetMapping("/delInfo")
    @ResponseBody
    public String delInfo(Integer id){
        try {
            boolean del = wxWdictionaryService.del(id);
            return JSONObject.toJSONString(del);
        } catch (Exception e) {
            e.printStackTrace();
            boolean b = false;
            return JSONObject.toJSONString(b);
        }
    }


    @RequestMapping("/updaInfo/{id}/{name}")
    public String toupdaInfo(@PathVariable Integer id,Model model,@PathVariable String name){
        AppInfo appInfo = wxWdictionaryService.selById(id);
        List<DataDictionary> data = wxWdictionaryService.selPing();
        List<AppCategory> selYi = wxWdictionaryService.selYi();
        Integer er = appInfo.getCategoryLevel1().intValue();
        Integer san = appInfo.getCategoryLevel2().intValue();
        List<AppCategory> selEr = wxWdictionaryService.selEr(er);
        List<AppCategory> selSan = wxWdictionaryService.selSan(san);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("data",data);
        model.addAttribute("selYi",selYi);
        model.addAttribute("selEr",selEr);
        model.addAttribute("selSan",selSan);
        model.addAttribute("zhunatai",name);
        return "developer/appinfomodify";
    }

    @RequestMapping("/upda")
    public String updaInfo(@ModelAttribute() AppInfo appInfo) {
        boolean b = wxWdictionaryService.updaInfo(appInfo);
        if (b){
            return "developer/appinfolist";
        }
        return "developer/appinfomodify";
    }

    /*@RequestMapping("/addInfo")
    public String addInfo(@ModelAttribute() AppInfo appInfo, HttpServletRequest request,
                          @RequestParam(value = "a_logoPicPath", required = false) MultipartFile attach){
        //1、定义保存到数据库的图片路径
        String picPath = null;

        //2、原文件路径
        String path = null;

        //3、文件名称
        String fileName = null;
        System.out.println(appInfo.getSoftwareName());
        System.out.println(appInfo.getSupportRom());
        System.out.println(appInfo.getSoftwareSize());
        System.out.println(appInfo.getDownloads());
        System.out.println(appInfo.getStatus());
        System.out.println(appInfo.getCategoryLevel1());
        System.out.println(appInfo.getCategoryLevel2());
        System.out.println(appInfo.getCategoryLevel3());
        System.out.println(appInfo.getFlatformId());
        System.out.println(appInfo.getAppInfo());
        System.out.println(appInfo.getLogoLocPath());
        System.out.println(appInfo.getLogoPicPath());
        //4、如果文件不为空
        *//*if (!attach.isEmpty()) {
            //5、获得文件源路径
            path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "upload");
            logger.info("uploadFile path ============== > " + path);

            //6、获得原文件名
            String oldFileName = attach.getOriginalFilename();
            logger.info("uploadFile oldFileName ============== > " + oldFileName);

            //7、获得原文件后缀
            String prefix = FilenameUtils.getExtension(oldFileName);
            logger.debug("uploadFile prefix============> " + prefix);

            //8、设置文件大小5000k,就是5mb
            int filesize = 5000000;
            logger.debug("uploadFile size============> " + attach.getSize());

            //9、如果源文件大小超过设定值
            if (attach.getSize() > filesize) {
                //10、进行文件过大提示
                request.setAttribute("uploadFileError", " * 上传大小不得超过 5mb");
                //11、返回添加页面
                return "developer/appinfoadd";

                //12、如果文件格式(忽略大小写的后缀)是apk格式
            } else if (prefix.equalsIgnoreCase("jpg")||prefix.equalsIgnoreCase("png")) {
                //13、给文件重命名为       当前系统的毫秒数+四位随机数 + .apk后缀
                fileName = System.currentTimeMillis() + RandomUtils.nextInt() + ".jpg";
                logger.debug("new fileName======== " + attach.getName());

                File targetFile = new File(path, fileName);
                //14、如果文件不存在,就生成此文件
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                //15、保存
                logger.debug("---------------fileName=" + fileName);

                try {
                    //16、保存文件
                    attach.transferTo(targetFile);
                } catch (Exception e) {
                    e.printStackTrace();
                    request.setAttribute("uploadFileError", " * 上传失败！");
                    return "developer/appinfoadd";
                }
                picPath = path + File.separator + fileName;
            } else {
                request.setAttribute("uploadFileError", " * 上传图片格式不正确");
                return "developer/appinfoadd";
            }
        }
        //17、获得源文件路径(下载地址)  新文件路径 赋值给对象的对应属性
        appInfo.setLogoLocPath(path);
        appInfo.setLogoPicPath(picPath);



        boolean bo= glservice.add(appInfo);

        logger.debug(bo);

        if (bo) {
            logger.debug("添加成功！"); //跳转到查询版本页面
            return "redirect:/info/sel";
        }else{
            logger.debug("添加失败！"); //跳转到查询版本页面
            request.setAttribute("uploadFileError", " * 上传失败！");
            return "developer/appinfoadd";
        }*//*

        return "developer/appinfoadd";
    }*/
    @RequestMapping("/toaddInfo")
    public String toaddInfo(){
        return "developer/appinfoadd";
    }
}


